package com.xingdata.jjxc.m.avt;

import jar.tools.UdpTools;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.activity.WlanListView;
import com.xingdata.jjxc.base.Base1Activity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.enty.UpdateInfoEntity;
import com.xingdata.jjxc.http.HttpResponseListener;
import com.xingdata.jjxc.http.HttpUtil;
import com.xingdata.jjxc.utils.JUtils;
import com.xingdata.jjxc.utils.StringUtil;
import com.xingdata.jjxc.utils.SystemInfo;
import com.xingdata.jjxc.views.CustomDialog;
import com.xingdata.jjxc.views.Widget;

/**
 * @author ysc 关于HUD
 */
public class Aboutavt extends Base1Activity implements OnClickListener {

	private RelativeLayout about_upgrade_rl, about_contact_us_rl;
	private TextView about_agreement_tv;
	//
	// // 当前版本号
	// public int versionCode = 0;
	// // 当前版本名称
	// private String versionName = "";
	// HUD版本号
	private String hud_version;

	private Dialog dialog_call;
	private String downloadUrl;

	private UpdateInfoEntity updateInfoEntity = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// getCurrentVersion();
		do_Post_softupdate();
	}

	@Override
	protected int getContentViewId() {
		return R.layout.activity_aboutavt;
	}

	@Override
	protected String getTitleText() {
		return JUtils.TITILE_JJXC;
	}

	@Override
	protected void initViews() {
		Typeface font = Typeface.createFromAsset(getAssets(), JUtils.TypefacePath);
		// 设置驾驾行车app版本号
		((TextView) findViewById(R.id.VersionName)).setText(SystemInfo.getAPKVersionName(this));
		about_upgrade_rl = (RelativeLayout) findViewById(R.id.about_upgrade_rl);
		about_contact_us_rl = (RelativeLayout) findViewById(R.id.about_contact_us_rl);
		about_agreement_tv = (TextView) findViewById(R.id.about_agreement_tv);

		// 设置字体
		((TextView) findViewById(R.id.Version_Text)).setTypeface(font);
		((TextView) findViewById(R.id.VersionName)).setTypeface(font);
		((TextView) findViewById(R.id.plam_tv)).setTypeface(font);
		((TextView) findViewById(R.id.connect_us)).setTypeface(font);
		((TextView) findViewById(R.id.phone_number)).setTypeface(font);
		((TextView) findViewById(R.id.about_agreement_tv)).setTypeface(font);
		((TextView) findViewById(R.id.copyright)).setTypeface(font);

		findViewById(R.id.new_flag).setOnClickListener(this);
		about_upgrade_rl.setOnClickListener(this);
		about_contact_us_rl.setOnClickListener(this);
		about_agreement_tv.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.new_flag:
			showUpadateDialog();
			break;
		case R.id.about_contact_us_rl:
			call();
			break;
		case R.id.about_agreement_tv:
			Intent intent = new Intent(Aboutavt.this, HUDWebView.class);
			Bundle mBundle = new Bundle();
			mBundle.putString("URL", App.JJ_AGREEMENT);
			intent.putExtras(mBundle);
			startActivity(intent);
			break;

		default:
			break;
		}
		Widget.startActivityAnim(this, R.anim.avty_slide_in_from_left, R.anim.avty_slide_out_from_right);

	}

	/** 打电话监听 */
	private OnClickListener listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.about_call:
				dialog_call.dismiss();
				Intent intent = new Intent();
				intent.setAction("android.intent.action.CALL");
				intent.addCategory("android.intent.category.DEFAULT");
				intent.setData(Uri.parse("tel: " + "4009699039"));
				startActivity(intent);
				break;
			case R.id.about_cancel:
				dialog_call.dismiss();
				break;

			default:
				break;
			}
		}
	};

	private void do_Post_softupdate() {
		String url = "http://jjxc.hudim.com/index.php/Hud/public/softupdate";
		paramsMap = new HashMap<String, String>();
		paramsMap.put("version", SystemInfo.getAPKVersionName(this));
		// paramsMap.put("version", "1.1.1");
		paramsMap.put("package_name", "com.xingdata.jjxc");
		paramsMap.put("platform", "Android");
		HttpUtil.getInstance(this).Post(url, paramsMap, new HttpResponseListener() {

			@Override
			public void onSuccess(RespEntity respEntity) {
				findViewById(R.id.new_flag).setVisibility(View.VISIBLE);
				updateInfoEntity = JSON.parseObject(respEntity.getResult(), UpdateInfoEntity.class);
				// Widget.checkVersion(Aboutavt.this,
				// resp.getState() + "", App.ZZD_VERSION_UPGRADE);
			}

			@Override
			public void onLoading(int vurrent) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onFailure(String result) {
				// TODO Auto-generated method stub

			}
		});
	}

	/** 点击打电话弹出底部弹出dialog */
	private void call() {
		if (dialog_call == null) {
			dialog_call = new Dialog(this, R.style.popup_fade_anim);
			// 设置显示动画
			Window window = dialog_call.getWindow();
			window.setWindowAnimations(R.style.popup_fade_anim);
			WindowManager.LayoutParams wl = window.getAttributes();
			wl.x = 0;
			wl.y = getWindowManager().getDefaultDisplay().getHeight();
			window.setAttributes(wl);
			dialog_call.onWindowAttributesChanged(wl);
			dialog_call.setCanceledOnTouchOutside(true);
			
			View v = LayoutInflater.from(this).inflate(R.layout.popup_call, null);
			
			View blank = v.findViewById(R.id.about_blank_view);
			blank.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					dialog_call.dismiss();
				}
			});
			Button call = (Button) v.findViewById(R.id.about_call);
			Button cancel = (Button) v.findViewById(R.id.about_cancel);
			call.setOnClickListener(listener);
			cancel.setOnClickListener(listener);
			
			LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			dialog_call.addContentView(v, params);
		}
		if (!this.isFinishing() && dialog_call != null && !dialog_call.isShowing()) {
			dialog_call.show();
		}
	}

	// /**
	// * 获得当前版本信息
	// */
	//
	// public void getCurrentVersion() {
	// try {
	// // 获取应用包信息
	// PackageInfo info = this.getPackageManager().getPackageInfo(
	// this.getPackageName(), 0);
	// this.versionCode = info.versionCode;
	// this.versionName = info.versionName;
	// } catch (NameNotFoundException e) {
	// e.printStackTrace();
	// }
	//
	// if (UdpTools.getHudVersion() != null
	// && UdpTools.getHudVersion().length() != 0) {
	// hud_versionTextView.setText(UdpTools.getHudVersion());
	// }
	// }

	public void showUpadateDialog() {
		CustomDialog.Builder builder = new CustomDialog.Builder(Aboutavt.this, R.layout.dialog_update_info);
		StringBuffer buffer = new StringBuffer();
		buffer.append("发现新版本" + updateInfoEntity.getVersion_no() + "\n\n");
		buffer.append("升级内容 " + "\n");
		buffer.append(updateInfoEntity.getVersion_desc().replaceAll(" ", "\n") + "\n");
		buffer.append("总大小：" + StringUtil.formatSize(updateInfoEntity.getFile_size()));
		builder.setMessage(buffer.toString());
		builder.setTitle("提示");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(updateInfoEntity.getVersion_path())));
				dialog.dismiss();
			}
		});

		builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});

		builder.create().show();
	}
}
